package top.deepdesigner.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import top.deepdesigner.pojo.DtsAdmin;
import top.deepdesigner.service.DtsAdminService;
import top.deepdesigner.util.bcrypt.BCryptPasswordEncoder;

import java.util.Date;
import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/20 星期二 16:51:56 <br/>
 */
@Service
public class DtsAdminServiceImpl extends ServiceImpl<BaseMapper<DtsAdmin>, DtsAdmin> implements DtsAdminService {
  /**
   * 找到dts管理员用户名
   * 根据用户名找管理员信息
   *
   * @param username 用户名
   * @return {@link DtsAdmin}
   */
  @Override
  public DtsAdmin findDtsAdminByUsername(String username) {
    LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(true, DtsAdmin::getUsername, username).eq(true, DtsAdmin::getDeleted, 0);
    return this.getOne(wrapper);
  }

  @Override
  public Long getAdminCount() {
    return this.count();
  }

  @Override
  public List<DtsAdmin> getAdminList(Integer page, Integer limit, String sort, String order, String username) {
    QueryWrapper<DtsAdmin> wrapper = new QueryWrapper<>();
    wrapper
        .eq("deleted", 0)
        .like(!StringUtils.isEmpty(username), "username", username)
        .orderBy(!StringUtils.isEmpty(order), "asc".equalsIgnoreCase(order), sort);
    Page<DtsAdmin> adminPage = new Page<>(page, limit);
    return this.page(adminPage, wrapper).getRecords();
  }

  @Override
  public List<DtsAdmin> getAdminList() {
    return this.list();
  }

  @Override
  @Transactional
  public DtsAdmin createAdmin(DtsAdmin dtsAdmin) {
    //判断用户是否存在并且未被删除
    LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<DtsAdmin>()
        .eq(!StringUtils.isEmpty(dtsAdmin.getUsername()), DtsAdmin::getUsername, dtsAdmin.getUsername())
        .eq(DtsAdmin::getDeleted, 0);
    List<DtsAdmin> list = this.list(wrapper);
    if (list != null && list.size() > 0) {
      return null;
    }
    dtsAdmin.setAddTime(new Date());
    dtsAdmin.setUpdateTime(new Date());
    //密码加密
    dtsAdmin.setPassword(new BCryptPasswordEncoder().encode(dtsAdmin.getPassword()));
    this.save(dtsAdmin);
    return dtsAdmin;
  }

  @Override
  @Transactional
  public DtsAdmin updateAdmin(DtsAdmin dtsAdmin) {
    LambdaUpdateWrapper<DtsAdmin> wrapper = new LambdaUpdateWrapper<>();
    wrapper.eq(true, DtsAdmin::getId, dtsAdmin.getId());
    this.update(dtsAdmin, wrapper);
    return dtsAdmin;
  }

  @Override
  @Transactional
  public void deleteAdmin(Integer id) {
    this.update(new LambdaUpdateWrapper<DtsAdmin>().set(DtsAdmin::getDeleted, 1).eq(DtsAdmin::getId, id));
  }

  @Override
  @Transactional
  public void updatePassword(Integer adminId, String newPassword) {
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    String newPasswd = encoder.encode(newPassword);
    DtsAdmin dtsAdmin = new DtsAdmin();
    dtsAdmin.setUpdateTime(new Date());
    dtsAdmin.setPassword(newPasswd);
    this.update(dtsAdmin, new LambdaUpdateWrapper<DtsAdmin>().eq(DtsAdmin::getId, adminId));
  }
}
